﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.Framework.Organization;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.Entity;


public partial class Page_NewTrainCredit : BaseAuthenticationPage
{
    private Guid CreditID
    {
        get
        {
            if (Request["CreditID"] != null)
            {
                return new Guid(Request["CreditID"]);

            }
            return Guid.Empty;
        }
    }

    protected DataTable DTClassOrder
    {
        get
        {
            if (ViewState["DTClassOrder"] != null)
            {
                return (DataTable)ViewState["DTClassOrder"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTClassOrder"] = value;
        }
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "新建培训学分";


        if (!IsPostBack)
        {
            if (CreditID != Guid.Empty)
            {

                TrainCredit trainCredit = new TrainCredit(CreditID);
                if (trainCredit.Load())
                {

                    if (!string.IsNullOrEmpty(trainCredit.BeginTime.ToString()))
                    {
                        tbBeginTime.Text = Convert.ToDateTime(trainCredit.BeginTime).ToShortDateString();
                    }
                    if (!string.IsNullOrEmpty(trainCredit.EndTime.ToString()))
                    {
                        tbEndTime.Text = Convert.ToDateTime(trainCredit.EndTime).ToShortDateString();
                    }
                    tbCreditName.Text = trainCredit.CreditName;
                    tbComment.Text = WebCommon.GetStandardStyleStr(trainCredit.Comment);
                    tbCourseScore.Text = trainCredit.Score.ToString();
                    tbMainContent.Text = trainCredit.MainContent;
                    tbHostUnit.Text = trainCredit.HostUnit;
                    tbTrainDayCount.Text = trainCredit.TrainDayCount.ToString();

                }
                this.Title = "编辑培训学分";
            }
        }
    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        TrainCredit trainCredit = new TrainCredit(CreditID);
        if (!trainCredit.Load())
        {
            trainCredit.ID = Guid.NewGuid();
            tbClassID.Text = trainCredit.ID.ToString();
            trainCredit.CreateTime = DateTime.Now;
            trainCredit.CreateUser = CurrentUser.DisplayName;
            trainCredit.CreateUserID = CurrentUser.ID;
            Department dept = CurrentUser.GetMainDepartment();
            if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.MinistryAdminRoleID))
            {
                trainCredit.CreditType = "人事部";
                //trainCredit.Status = "待审核";
                //修改为 初审通过
                trainCredit.Status = "初审通过";
            }
            else if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID))
            {
                trainCredit.CreditType = "区组织部";
                trainCredit.Status = "终审通过";
            }
            else
            {
                trainCredit.CreditType = "个人";
                trainCredit.Status = "待审核";
            }
        }
        else
        {
            tbClassID.Text = CreditID.ToString();
        }
        try
        {
            trainCredit.BeginTime = Convert.ToDateTime(tbBeginTime.Text);
        }
        catch (Exception)
        {
            trainCredit.BeginTime = DateTime.MinValue;
        }
        try
        {
            trainCredit.EndTime = Convert.ToDateTime(tbEndTime.Text);
        }
        catch (Exception)
        {
            trainCredit.EndTime = DateTime.MinValue;
        }
        trainCredit.TrainLevel = ddlTrainLevel.SelectedValue == "请选择" ? string.Empty : ddlTrainLevel.SelectedValue;
        trainCredit.Comment = WebCommon.GetHTMLStyleStr(tbComment.Text);
        trainCredit.CreditName = tbCreditName.Text;
        trainCredit.MainContent = tbMainContent.Text;
        float trainday = 0;
        float.TryParse(tbTrainDayCount.Text, out trainday);
        trainCredit.TrainDayCount = trainday;
        trainCredit.HostUnit = tbHostUnit.Text;
        try
        {
            trainCredit.Score = Convert.ToDouble(tbCourseScore.Text);
        }
        catch (Exception)
        {
            trainCredit.Score = 0;
        }
        trainCredit.Save();
        if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID) && CreditID == Guid.Empty)
        {
            BLTrain.AddCreditScore(trainCredit);
        }
        string script = "<script>alert('保存成功!');refreshParent();window.close();</script>";
        ClientScript.RegisterStartupScript(this.GetType(), "Close", script);
    }

}
